package org.fdy.common.framework.config;

import lombok.RequiredArgsConstructor;
import org.fdy.common.framework.config.properties.ThreadProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * <p>说明：线程池配置 </P>
 *
 * @author: fangdayang
 * @create: 2023-10-20 00:20
 **/
@Configuration
@RequiredArgsConstructor
public class ThreadConfiguration {

    private final ThreadProperties threadProperties;

    @Bean(name = "threadPoolExecutor")
    public ThreadPoolExecutor threadPoolExecutor() {
        return new ThreadPoolExecutor(threadProperties.getCorePoolSize(),
                threadProperties.getMaximumPoolSize(),
                threadProperties.getKeepAliveTime(), TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(threadProperties.getWorkQueue()), threadFactory());
    }
    private ThreadFactory threadFactory() {
        return new CustomizableThreadFactory(threadProperties.getThreadNamePrefix());
    }
}
